package com.qidianit.mes.sys.controller;

import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.Supplier;
import com.qidianit.mes.sys.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description:
 * @Author: shanzhe
 * @Date: 2025/9/16 - 20:05
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    @GetMapping("/page")
    public Result<PageInfo<Supplier>> selectSuppliersByConditionsWithPaging(@RequestParam(value = "code",required = false) String code,
                                                                            @RequestParam(value = "name",required = false) String name,
                                                                            @RequestParam(value = "status",required = false) Integer status,
                                                                            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                                            @RequestParam(value = "size",defaultValue = "10") Integer size)
    {
        PageInfo<Supplier> supplierPageInfo =  supplierService.selectSuppliersByConditionsWithPaging(code,name,status,pageNum,size);
        return Result.success(supplierPageInfo);
    }


    @GetMapping("/code/{code}")
    public Result selectSupplierByCode(@PathVariable String code) {
        Supplier supplier = supplierService.selectSupplierByCode(code);
        return Result.success(supplier);
    }

    @PostMapping("/add")
    public Result addSupplier(@RequestBody Supplier supplier) {
        supplier.setId(13L);
        supplier.setCreateTime(LocalDateTime.now());
        int result = supplierService.addSupplier(supplier);
        return result == 1?Result.success(supplier):Result.error("操作失败");
    }

    @PutMapping("/update")
    public Result updateSupplier(@RequestBody Supplier supplier) {
        supplier.setUpdateTime(LocalDateTime.now());
        int result = supplierService.updateSupplierById(supplier);
        return  result == 1?Result.success():Result.error("操作失败");
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteSupplierById(@PathVariable Integer id) {
        int result = supplierService.deleteSupplierById(id);
        return result == 1?Result.success():Result.error("操作失败");
    }

    @DeleteMapping("/batchDelete")
    public Result deleteSupplierByIds(@RequestParam("id") List<Integer> ids) {
        int result = supplierService.deleteSupplierByIds(ids);
        return result == 1?Result.success():result == -1?Result.error("没有删全"):Result.error("操作失败");
    }
}
